﻿using Ldt.DataEngine.DataFlowParse.Model;
using LdtDataEngin.ModuleDetail.CommonModule.PersonMerger;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 人员代称替换
    /// </summary>
    public class MoudleReplace : MoudleBase
    {
        private IMoudle _moudle;
        private static string _moudleResult;
        /// <summary>
        /// 人员代称替换
        /// </summary>
        /// <param name="moudle">下一个模块</param>
        public MoudleReplace(IMoudle moudle)
        {
            _moudle = moudle;
        }
        /// <summary>
        /// 执行模块数据
        /// </summary>
        /// <param name="dataContext"></param>
        public override void MoudleHandle(DataContext dataContext)
        {
            string moudleName = typeof(MoudleReplace).FullName;
            moudleName += "_" + this.GetHashCode();
            // 设置模块进度
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
            // 创建计时器
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            try
            {
                XmlUtilCommon xmlUtilCommon = new XmlUtilCommon();
                XmlDocument xmlDocumentOp = new XmlDocument();
                xmlDocumentOp = xmlUtilCommon.DataModelByXml(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, "");
                dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->开始执行人员代称替换模块*****************************");
                string xmlconfig = "";
                var configmodel = dataContext.DataLayerSys.GetConfigModel(moudleName);
                if (configmodel!=null)
                {
                    //获取人员转换配置
                    ConfigModel pronounConfig = configmodel.ConfigDetail.FirstOrDefault(x=>x.Code.ToLower()== "persontransform");
                    if(pronounConfig==null)
                        pronounConfig = configmodel.ConfigDetail.FirstOrDefault(x => x.Code.ToLower() == "shortnamereplace");
                    xmlconfig = pronounConfig?.ConfigContent;
                }
                PersonnelDeductionData personnelDeductionData = new PersonnelDeductionData() { dynamicDataStores = dataContext };
                personnelDeductionData.PersonnelAnalysis(xmlconfig, dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList);
                _moudleResult = dataContext.DataLayerAns.AnalysisResultsXml.OuterXml;
                xmlDocumentOp = xmlUtilCommon.DataModelByXml(dataContext.DataLayerAns.AnalysisResultsDataModel.dataModelList, "");

            }
            catch (Exception ex)
            {
                dataContext.InteractiveShow.LogShow("人员代称替换失败！错误消息为：" + ex.Message, LogMessageType.Error);
            }
            dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->人员代称替换模块执行完成*****************************");
            // 设置模块进度
            stopwatch.Stop();
           
            dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            //执行完毕调用下一个模块；
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if (!string.IsNullOrWhiteSpace(_moudleResult))
            {

                resultModel.ResultType = "1";
                resultModel.ResultData = _moudleResult;
            }
            return resultModel;
        }

    }
}
